Show language: C# VB.NET Both
This Windows Forms example is intended to demonstrate a basic programmatic implementation. From the button click event, the website source is imported and the index fully built ready for searching.
Reference these DLL's in your project, KeyotiX.SearchEngine.Core, KeyotiX.SearchEngine.License, KeyotiX.Text.LemmaGenerator.dll, KeyotiX.Text.MSOffice.dll - where X is 2 for .NET 2 and 4 for .NET 4
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Keyoti.SearchEngine;
using Keyoti.SearchEngine.Index;
using Keyoti.SearchEngine.DataAccess.IndexableSourceRecords;
using Keyoti.SearchEngine.Search;
namespace WindowsApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Configuration configuration = new Configuration();
configuration.IndexDirectory = "C:/YourSite/IndexDirectory";
//---Import pages from website.
DocumentIndex documentIndex = new DocumentIndex(configuration);
try{
documentIndex.ImportWebsite("http://www.yoursite.com");
} finally {
documentIndex.Close();
}
//---Search Index
SearchAgent sa = new SearchAgent("EnterLicenseKeyHere", configuration);
SearchResult res = sa.Search("search terms", 1, 10); //res now holds a collection of ResultItem objects
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports Keyoti.SearchEngine
Imports Keyoti.SearchEngine.Index
Imports Keyoti.SearchEngine.DataAccess.IndexableSourceRecords
Imports Keyoti.SearchEngine.Search
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim configuration As Keyoti.SearchEngine.Configuration = New Configuration
configuration.IndexDirectory = "C:/YourSite/IndexDirectory"
'---Import pages from website.
Dim documentIndex As DocumentIndex = New DocumentIndex(configuration)
Try
documentIndex.ImportWebsite("http://www.yoursite.com")
Finally
documentIndex.Close()
End Try
'---Search Index
Dim sa As SearchAgent = New SearchAgent("EnterLicenseKeyHere", configuration)
Dim res As SearchResult = sa.Search("search terms", 1, 10) 'res now holds a collection of ResultItem objects
End Sub
End Class
The try-finally constructs ensure that the index is closed even if an exception occurs.
Instead of the ImportWebsite call in the code above, it is possible to add documents to the index incrementally. This is ideal for updating the index as documents are created/uploaded.
DocumentIndex documentIndex = new DocumentIndex(configuration);
try{
documentIndex.AddDocument(new Document("http://some/URL/document", configuration));
} finally {
documentIndex.Close();
}
Dim documentIndex As DocumentIndex = New DocumentIndex(configuration)
Try
documentIndex.AddDocument(new Document("http://some/URL/document", configuration))
Finally
documentIndex.Close()
End Try
Similarly use the RemoveDocument method in DocumentIndex to remove a document from the index. It's important that the document URL matches exactly with the URL already in the index. Please pay attention to trailing slashes (eg. http://localhost/) and ensure any spaces are encoded as %20.
When a row is imported from a DB, we create our own URI for it. To delete that row/document, you need to recreate the URI.
IndexableSourceUri uri = new IndexableSourceUri(1, "d4", "col1");
//where 1 is the IndexableSource ID (see below)
//"d4" is the value in the unique field, that identifies the row to delete
//"col1" is the name of the unique field
documentIndex.RemoveDocument(new Document(uri.UriInstance.AbsoluteUri, Configuration));
col1 data
-------------
a1 blah
b2 some
c3 empty
d4 more
so the code will remove that last row from the index.
The indexable source ID, can be obtained with code like this
ArrayList recs = documentIndex.GetIndexableSourceRecords();
(recs[0] as IndexableSourceRecord).ID;